
var vm = new Vue({
    el : "#fzJobList",
    data : {
        record: {},
        sels: [],
        flyWeavs: [],
        jobStatus: [],
        status: '',
        table: null,
        jobCode: '',
        flyWeav: '',
    },
    init: function(){
        var _self = this;

        this.$nextTick(function() {
            this.$http.post(host + "flyWeavController/selectFlyWeavs",{},{emulateJSON: true}).then(function(res){
                if(res.ok){
                    _self.flyWeavs = res.body.flyWeavs;

                    layui.use('form', function() {
                        var form = layui.form;
                        window.setTimeout(function() {
                            form.render('select');

                            form.on("select(status)", function(obj, val) {
                                _self.status = obj.value;
                                _self.query();
                            });
                            form.on("select(flyWeav)", function(obj) {
                                _self.flyWeav = obj.value;
                                _self.query();
                            });
                        }, 300);
                    });
                    _self.renderTable();
                }
            });
        });
    },
    methods: {
        query: function(){
            this.table.reload({page: {curr: 1}, where: {status: this.status, flyWeav: this.flyWeav, jobCode: this.jobCode}});
        },
        reset:function(){
            window.location.reload();
        },
        vueAlert:function(msg){
            layer.msg(msg,{icon:6, time: 2000});
        },
        renderTable: function(){
            var _self = this;
            layui.use(['form','table','laydate'], function(){
                layer = layui.layer;
                var table = layui.table;
                var form = layui.form;

                _self.table = table.render({
                    id: 'fzJobListTable',
                    elem: '#fzJobListTable',
                    loading: true,
                    even: true,
                    width: 'full-20',
                    height: 'full-150',
                    cols: [[ //标题栏
                        {type:' numbers'},
                        {field: 'CODE', title: '作业工单号',minWidth: 140},
                        {field: 'NAME', title: '型体',minWidth: 150, align:'center' },
                        {field: 'COLORNAME', title: '颜色', minWidth: 120, align:'center'},
                        {field: 'FZJ', title: '飞织机机台', minWidth: 120, align:'center'},
                        {field: 'POSITION', title: '机台AGV坐标', minWidth: 120, align:'center'},
                        {field: 'SIZE_CODE', title: '尺码', minWidth: 80, align:'center'},
                        {field: 'STATUS_XM', title: '状态', minWidth: 110, align:'center', templet: function(data) {
                            if (data.STATUS_XM == 'WRITER') {
                                return 'RFID已写入';
                            } else if (data.STATUS_XM == 'GET') {
                                return 'AGV取鞋面';
                            } else if (data.STATUS_XM == 'OK') {
                                return '鞋面已取完';
                            }
                            return '飞织完工';
                        }},
                        {field: 'EXE_DATE_FROM', title: '开工时间', minWidth: 160, align:'center', templet: function(data) {
                            if (data.EXE_DATE_FROM) {
                                return new Date(data.EXE_DATE_FROM).toString(true);
                            }
                            return '';
                        }},
                        {field: 'EXE_DATE_TO', title: '完工时间', minWidth: 160, align:'center', templet: function(data) {
                            if (data.EXE_DATE_TO) {
                                return new Date(data.EXE_DATE_TO).toString(true);
                            }
                            return '';
                        }},
                        {field: 'ID', title: '操作', minWidth: 200, align:'center',
                            templet: function(data) {
                                var html = '';
                                if (!data.STATUS_XM) {
                                    html = '<a class="layui-btn layui-btn-xs table_btn" lay-event="rfid">写入RFID</a>';
                                } else {
                                    html = '<a class="layui-btn layui-btn-xs table_btn" lay-event="rewriter">重写RFID</a>';
                                }

                                if (data.STATUS_XM == 'WRITER') {
                                    html += '<a class="layui-btn layui-btn-danger layui-btn-xs table_btn" lay-event="agv">取鞋面</a>';
                                } else if (data.STATUS_XM == 'GET') {
                                    html += '<a class="layui-btn layui-btn-danger layui-btn-xs table_btn" lay-event="ok">鞋面完成</a>';
                                }
                                return html;
                            }
                        }
                    ]],
                    method: "post",
                    url: host + "flyWeavController/selectFlyWeavList",
                    page: true, //是否显示分页
                    limits: [10, 20, 50],
                    limit: 10 //每页默认显示的数量
                });

                //监听工具条
                table.on('tool(remakeReceiveFilter)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                    var row = obj.data; //获得当前行数据
                    layEvent = obj.event; //获得 lay-event 对应的值
                    if(layEvent == 'agv') {
                        _self.callAgv(row);
                    } else if(layEvent == 'rfid') {
                        _self.updateJobStatus(row);
                    } else if(layEvent == 'ok') {
                        _self.setAgvEnd(row);
                    } else if (layEvent == '') {
                        _self.writeRfid(row);
                    }
                });
            });
        },
        callAgv: function(row) {
            var _self = this;
            _self.$nextTick(function() {
                _self.$http.post(host + "flyWeavController/callAgv",
                    {POSITION: row.POSITION, code: row.CODE, fzj: row.FZJ.substr(row.FZJ.length - 2) * 1},
                    {emulateJSON: true}).then(function(res){
                    if(res.ok){
                        if (res.body.data) {
                            var data = {};
                            eval("data=" + res.body.data);
                            if (data.task_seq == 1) {
                                layer.msg(res.body.msg + "，取料AGV：" + data.agv_id);
                            } else if (data.task_seq == 3) {
                                layer.msg("小车处于异常状态，无法移动！");
                            } else {
                                layer.msg(data.msg);
                            }
                        } else {
                            layer.msg(res.body.msg);
                        }
                        window.setTimeout(function() {
                            window.location.reload();
                        }, 1000);
                    }
                });
            });
        },
        writeRfid: function(row) {
            //配置一个透明的询问框
            top.layer.open({
                type: 2,
                title: "写入RFID",
                shadeClose: true,
                shade: 0.6,
                area: ['300', '120'],
                fixed:true,
                content: 'http://localhost:28088/xdRfid/write?rfid=' + row.CODE,
                end:function() {
                }
            });
        },
        setAgvEnd: function(row) {
            this.$nextTick(function() {
                //配置一个透明的询问框
                this.$http.post(host + "flyWeavController/setAgvEnd",
                    {code: row.CODE, POSITION: row.POSITION, fzj: row.FZJ.substr(row.FZJ.length - 2) * 1},
                    {emulateJSON: true}).then(function(res) {
                    if(res.ok) {
                        layer.msg("鞋面已取，AGV已设置为空闲！");
                        window.setTimeout(function() {
                            window.location.reload();
                        }, 1000);
                    }
                });
            });
        },
        updateJobStatus: function(row) {
            this.writeRfid(row);

            this.$nextTick(function() {
                //修改工单状态
                this.$http.post(host +"flyWeavController/updateJobStatus",
                    {jobCode: row.CODE, POSITION: row.POSITION, fzj: row.FZJ.substr(row.FZJ.length - 2) * 1},
                    {emulateJSON: true}).then(function(res){
                    if(res.ok) {
                        window.setTimeout(function() {
                            window.location.reload();
                        }, 1000);
                    }else{
                        layer.msg(res.body.msg);
                    }
                });
            });
        }
    }
});